package com.sky.happygos.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.TResult;
import com.sky.happygos.R;
import com.sky.happygos.base.BaseA;
import com.sky.happygos.base.ImagePath;
import com.sky.happygos.common.LoadView;
import com.sky.happygos.event.CreateGoodsE;
import com.sky.happygos.event.GoodsE;
import com.sky.happygos.model.Goods;
import com.sky.happygos.present.GoodsP;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import static com.sky.happygos.R.id.header_title;

/**
 * Created by qqqqqqq on 2017/4/14.
 */
@EActivity(R.layout.activity_create_goods)
public class CreateGoodsA extends TakePhotoActivity {
    public static final String EXTRA_CREATE_SHOPID = "EXTRA_CREATE_SHOPID";
    public static final String EXTRA_CREATE_GOODSID = "EXTRA_CREATE_GOODSID";

    @ViewById(R.id.sdv_create_goods_icon)
    SimpleDraweeView image;
    @ViewById(R.id.et_create_goods_name)
    EditText name;
    @ViewById(R.id.et_create_goods_info)
    EditText info;
    @ViewById(R.id.et_create_goods_price)
    EditText price;
    @ViewById(R.id.et_create_goods_count)
    EditText count;
    @ViewById(R.id.et_create_goods_type)
    EditText type;
    @ViewById(R.id.loadView)
    LoadView loadView;
    @ViewById(R.id.header_title)
    TextView title;

    @Extra(EXTRA_CREATE_SHOPID)
    String shopid;

    @Extra(EXTRA_CREATE_GOODSID)
    String goodsid;

    @Bean
    GoodsP goodsP;

    Goods good;

    String path = null;

    @Click(R.id.header_left)
    public void back(){
        finish();
    }


    @AfterViews
    public void init(){
        if(goodsid != null){
            title.setText("编辑商品");
            loadView.start();
            goodsP.getGoodsById(goodsid);
        }
    }


    @Subscribe
    public void goodsResult(GoodsE e){
        loadView.dismiss();
        if(e.isSuccess){
            good = e.data;
            refreshView();
        }else{
            Toast.makeText(this,"获取商品失败",Toast.LENGTH_SHORT).show();
        }
    }

    public void refreshView(){
        image.setImageURI(ImagePath.to(good.icon));
        name.setText(good.name);
        info.setText(good.info);
        price.setText(String.format("%.2f", good.price));
        count.setText(String.format("%d", good.count));
        type.setText(String.format("%d", good.type));
    }



    @Click(R.id.sdv_create_goods_icon)
    public void clickPhoto(){
        TakePhoto takePhoto = getTakePhoto();
        CompressConfig compressConfig=new CompressConfig.Builder().setMaxSize(600*800).setMaxPixel(800).create();
        takePhoto.onEnableCompress(compressConfig,true);
        takePhoto.onPickFromGallery();
    }

    @Click(R.id.rl_create_goods_finish)
    public void clickFinish(){
        if(goodsid != null){
            try {
                Goods goods = new Goods();
                goods.id = goodsid;
                goods.name = name.getText().toString();
                goods.info = info.getText().toString();
                goods.price = Float.parseFloat(price.getText().toString());
                goods.count = Integer.parseInt(count.getText().toString());
                goods.type = Integer.parseInt(type.getText().toString());
                loadView.start();
                goodsP.updateGoods(path,goods);
            } catch (NumberFormatException e) {
                Toast.makeText(this, "价格/库存/类型应该为数字类型", Toast.LENGTH_SHORT).show();
            }
        }else {
            try {
                Goods goods = new Goods();
                goods.name = name.getText().toString();
                goods.info = info.getText().toString();
                goods.price = Float.parseFloat(price.getText().toString());
                goods.count = Integer.parseInt(count.getText().toString());
                goods.type = Integer.parseInt(type.getText().toString());
                loadView.start();
                goodsP.createGoods(path, shopid, goods);
            } catch (NumberFormatException e) {
                Toast.makeText(this, "价格/库存/类型应该为数字类型", Toast.LENGTH_SHORT).show();
            }
        }

    }
    @Subscribe
    public void CreateGoodsResult(CreateGoodsE e){
        loadView.dismiss();
        if(e.isSuccess){
            if(goodsid != null) {
                Toast.makeText(this, "修改商品成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "添加商品成功", Toast.LENGTH_SHORT).show();
            }
            finish();
        }else{
            if(goodsid != null) {
                Toast.makeText(this,"修改商品失败:"+e.error.message,Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this,"添加商品失败:"+e.error.message,Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        path = result.getImage().getCompressPath();
        image.setImageURI(String.format("file://%s",result.getImage().getCompressPath()));
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
        Toast.makeText(this,"获取失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void takeCancel() {
        super.takeCancel();
        Toast.makeText(this,"获取取消",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
